/*
 ----------------------------------------------------------------------------------
                          "THE BEER-WARE COPYRIGHT LICENSE"
                                    (Revision 43)
                                     -----------
   Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
   this file has been sent directly to You by the author, You can do whatever You
   want with this stuff as long as You don't sell or rent it. If we meet some day,
   and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                __________
                                                                Paweł Rymaszewski
 ----------------------------------------------------------------------------------
 */

package ui.windows.components.models;

import javax.swing.*;
import java.awt.event.KeyEvent;

/**
 * Class represents Integer only JText field.
 * Created to avoid unnnecessary validation of models
 * fields in search parameters panel.
 *
 * @author rymek
 *         User: Rymek
 *         Date: 23.07.11
 *         Time: 17:39
 */
public class IntegerTextField extends JTextField {

    private final static String badchars = "`~!@#$%^&*()_+=\\|\"':;?/>.<, ";

    @Override
    /**
     * Consumes every char tat is not allowed
     * by this models field
     */
    public void processKeyEvent(KeyEvent ev) {

        char c = ev.getKeyChar();

        if ((Character.isLetter(c) && !ev.isAltDown())
                || badchars.indexOf(c) > -1) {
            ev.consume();
            return;
        }
        if (c == '-' && getDocument().getLength() > 0)
            ev.consume();
        else super.processKeyEvent(ev);
    }
}
